import { Injectable } from '@nestjs/common';
import { SignatureDTO } from './model/common';
import * as crypto from 'crypto';
import { AppKey } from './common/define';

@Injectable()
export class AppService {

  // constructor(
  //   private readonly wxmongoService: WxmongoService,
  // ) {}

  // // 获取日志列表
  // getLogList() {
  //   return this.wxmongoService.create({url: 'String', time: 'String', data: 'String'});
  // }
  getHello(): string {
    return 'Hello World!';
  }
  // 检测签名
  checkSignature(query: SignatureDTO) {

    const signa = query.signature;
    const timestamp = query.timestamp;
    const nonce = query.nonce;

    const token = AppKey.WX.defaultToken;
    const signList = [token, timestamp, nonce];
    const sortlist = signList.sort();
    const signStr = sortlist.join('');

    const hash = crypto.createHash('sha1');
    const hashStr = hash.update(signStr).digest('hex');

    if (hashStr === signa) {
      return query.echostr;
    } else {
      return '';
    }
  }

}
